home *** CD-ROM | disk | FTP | other *** search
/ T&A 2 the Maxx 3 / T and A 2 The Maxx Number 3.iso / viewers / unixview / xgiftar.z / xgiftar / misc.c < prev    next >
C/C++ Source or Header  |  1991-05-20  |  9KB  |  336 lines

  1. /* misc.c:
  2.  *
  3.  * miscellaneous funcs
  4.  *
  5.  * jim frost 10.05.89
  6.  *
  7.  * Copyright 1989, 1990, 1991 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "xloadimage.h"
  13. #include "patchlevel"
  14. #include <signal.h>
  15.  
  16. extern int      _Xdebug;
  17. extern Display *Disp;
  18. extern int      Scrn;
  19.  
  20. static char *signalName(sig)
  21.      int sig;
  22. { static char buf[32];
  23.  
  24.   switch (sig) {
  25.   case SIGSEGV:
  26.     return("SEGV");
  27.   case SIGBUS:
  28.     return("BUS");
  29.   case SIGFPE:
  30.     return("FPE");
  31.   case SIGILL:
  32.     return("ILL");
  33.   default:
  34.     sprintf(buf, "Signal %d", sig);
  35.     return(buf);
  36.   }
  37. }
  38.  
  39. void memoryExhausted()
  40. {
  41.   fprintf(stderr,
  42.       "Memory has been exhausted; operation cannot continue (sorry).\n");
  43.   if (_Xdebug)
  44.     abort();
  45.   else
  46.     exit(1);
  47. }
  48.  
  49. void internalError(sig)
  50.      int sig;
  51. { static int handling_error= 0;
  52.   int a, b;
  53.   Screen *screen;
  54.  
  55.   switch(handling_error++) {
  56.   case 0:
  57.     printf("\
  58. An internal error (%s) has occurred.  If you would like to file a bug\n\
  59. report, please send email to %s\n\
  60. with a description of how you triggered the bug, the output of xloadimage\n\
  61. before the failure, and the following information:\n\n", signalName(sig),
  62.        AUTHOR_EMAIL);
  63.     printf("Xloadimage Version %s.%s\n", VERSION, PATCHLEVEL);
  64.     if (Disp) {
  65.       screen= ScreenOfDisplay(Disp, Scrn);
  66.       printf("Server: %s Version %d\n", ServerVendor(Disp), VendorRelease(Disp));
  67.       printf("Depths and visuals supported:\n");
  68.       for (a= 0; a < screen->ndepths; a++) {
  69.     printf("%2d:", screen->depths[a].depth);
  70.     for (b= 0; b < screen->depths[a].nvisuals; b++)
  71.       printf(" %s", nameOfVisualClass(screen->depths[a].visuals[b].class));
  72.     printf("\n");
  73.       }
  74.     }
  75.     else
  76.       printf("[No information on server; error occurred before connection]\n");
  77.     break;
  78.   case 1:
  79.     fprintf(stderr, "\
  80. An internal error has occurred within the internal error handler.  No more\n\
  81. information about the error is available, sorry.\n");
  82.     break;
  83.   }
  84.   if (_Xdebug) /* dump core if -debug is on */
  85.     abort();
  86.   exit(1);
  87. }
  88.  
  89. void version()
  90. {
  91.   printf("Xloadimage version %s patchlevel %s by Jim Frost.\n",
  92.      VERSION, PATCHLEVEL);
  93.   printf("Please send email to %s for\npraise or bug reports.\n",
  94.      AUTHOR_EMAIL);
  95. }
  96.  
  97. void usage(name)
  98.      char *name;
  99. {
  100.   version();
  101.   printf("\nUsage: %s [global options] {[image options] image_name ...}\n\n",
  102.      tail(name));
  103.   printf("\
  104. Type `%s -help [option ...]' for information on a particular option, or\n\
  105. `%s -help' to enter the interactive help facility.\n", tail(name), tail(name));
  106.   exit(1);
  107. }
  108.  
  109. char *tail(path)
  110.      char *path;
  111. { int   s;
  112.   char *t;
  113.  
  114.   t= path;
  115.   for (s= 0; *(path + s) != '\0'; s++)
  116.     if (*(path + s) == '/')
  117.       t= path + s + 1;
  118.   return(t);
  119. }
  120.  
  121. Image *processImage(disp, scrn, image, options, verbose)
  122.      Display      *disp;
  123.      int           scrn;
  124.      Image        *image;
  125.      ImageOptions *options;
  126.      unsigned int  verbose;
  127. { Image        *tmpimage;
  128.   XColor        xcolor;
  129.   unsigned int  compressed= 0;
  130.   void          gammacorrect();
  131.  
  132.   goodImage(image, "processImage");
  133.  
  134.   /* clip the image if requested
  135.    */
  136.  
  137.   if ((options->clipx != 0) || (options->clipy != 0) ||
  138.       (options->clipw != 0) || (options->cliph != 0)) {
  139.     if (!options->clipw)
  140.       options->clipw= image->width;
  141.     if (!options->cliph)
  142.       options->cliph= image->height;
  143.     tmpimage= clip(image, options->clipx, options->clipy,
  144.            (options->clipw ? options->clipw : image->width),
  145.            (options->cliph ? options->cliph : image->height),
  146.            verbose);
  147.     freeImage(image);
  148.     image= tmpimage;
  149.   }
  150.  
  151.   if (options->rotate) {
  152.     tmpimage = rotate(image, options->rotate, verbose);
  153.     freeImage(image);
  154.     image = tmpimage;
  155.   }
  156.  
  157.   if (options->xzoom || options->yzoom) { /* zoom image */
  158.     if (!options->colors && RGBP(image) &&             /* if the image is to */
  159.     (!options->xzoom && (options->yzoom > 100)) || /* be blown up, */
  160.     (!options->yzoom && (options->xzoom > 100)) || /* compress before */
  161.     (options->xzoom + options->yzoom > 200)) {     /* doing it */
  162.       compress(image, verbose);
  163.       compressed= 1;
  164.     }
  165.     tmpimage= zoom(image, options->xzoom, options->yzoom, verbose);
  166.     freeImage(image);
  167.     image= tmpimage;
  168.   }
  169.  
  170.   if (options->gray) /* convert image to grayscale */
  171.     gray(image, verbose);
  172.  
  173.   if (options->normalize) { /* normalize image */
  174.     tmpimage= normalize(image, verbose);
  175.     if (tmpimage != image) {
  176.       freeImage(image);
  177.       image= tmpimage;
  178.     }
  179.   }
  180.  
  181.   if (options->bright) /* alter image brightness */
  182.     brighten(image, options->bright, verbose);
  183.  
  184.   if (options->gamma != 1.0) /* do display gamma compensation */
  185.     gammacorrect(image, options->gamma, verbose);
  186.  
  187.   /* forcibly reduce colormap
  188.    */
  189.  
  190.   if (options->colors && RGBP(image) && (options->colors < image->rgb.used)) {
  191.     tmpimage= reduce(image, options->colors, verbose);
  192.     if (tmpimage != image) {
  193.       freeImage(image);
  194.       image= tmpimage;
  195.     }
  196.     image->rgb.size= options->colors; /* lie */
  197.     compressed= 1;
  198.   }
  199.  
  200.   if (options->dither && (image->depth > 1)) { /* image is to be dithered */
  201.     if (options->dither == 1)
  202.       tmpimage= dither(image, verbose);
  203.     else
  204.       tmpimage= halftone(image, verbose);
  205.     freeImage(image);
  206.     image= tmpimage;
  207.   }
  208.   else if (!compressed)       /* make sure colormap is minimized */
  209.     compress(image, verbose);
  210.  
  211.   if (options->smooth > 0) { /* image is to be smoothed */
  212.     tmpimage= smooth(image, options->smooth, verbose);
  213.     if (tmpimage != image) {
  214.       freeImage(image);
  215.       image= tmpimage;
  216.     }
  217.   }
  218.  
  219.   /* set foreground and background colors of mono image
  220.    */
  221.  
  222.   xcolor.flags= DoRed | DoGreen | DoBlue;
  223.   if ((image->depth == 1) && options->fg) {
  224.     XParseColor(disp, DefaultColormap(disp, scrn), options->fg, &xcolor);
  225.     *(image->rgb.red + 1)= xcolor.red;
  226.     *(image->rgb.green + 1)= xcolor.green;
  227.     *(image->rgb.blue + 1)= xcolor.blue;
  228.   }
  229.   if ((image->depth == 1) && options->bg) {
  230.     XParseColor(disp, DefaultColormap(disp, scrn), options->bg, &xcolor);
  231.     *image->rgb.red= xcolor.red;
  232.     *image->rgb.green= xcolor.green;
  233.     *image->rgb.blue= xcolor.blue;
  234.   }
  235.   return(image);
  236. }
  237.  
  238. /* simple error handler.  this provides us with some kind of error recovery.
  239.  */
  240.  
  241. int errorHandler(disp, error)
  242.      Display *disp;
  243.      XErrorEvent *error;
  244. { char errortext[BUFSIZ];
  245.  
  246.   XGetErrorText(disp, error->error_code, errortext, BUFSIZ);
  247.   fprintf(stderr, "xloadimage: X Error: %s on 0x%x\n",
  248.       errortext, error->resourceid);
  249.   if (_Xdebug) /* if -debug mode is enabled, dump a core when we hit this */
  250.     abort();
  251.   else
  252.     return(0);
  253. }
  254.  
  255. /*
  256.   strstr - public-domain implementation of standard C library function
  257.  
  258.   last edit:    02-Sep-1990    D A Gwyn
  259.  
  260.   This is an original implementation based on an idea by D M Sunday,
  261.   essentially the "quick search" algorithm described in CACM V33 N8.
  262.   Unlike Sunday's implementation, this one does not wander past the
  263.   ends of the strings (which can cause malfunctions under certain
  264.   circumstances), nor does it require the length of the searched
  265.   text to be determined in advance.  There are numerous other subtle
  266.   improvements too.  The code is intended to be fully portable, but in
  267.   environments that do not conform to the C standard, you should check
  268.   the sections below marked "configure as required".  There are also
  269.   a few compilation options, as follows:
  270. */
  271.  
  272. #ifndef UCHAR_MAX
  273. #define UCHAR_MAX 255
  274. #endif
  275.  
  276. typedef unsigned char cuc;    /* char variety used in algorithm */
  277.  
  278. #define EOS '\0'        /* C string terminator */
  279.  
  280. char *                    /* returns -> leftmost occurrence,
  281.                        or null pointer if not present */
  282. strstr( s1, s2 )
  283.      char    *s1;        /* -> string to be searched */
  284.      char    *s2;        /* -> search-pattern string */
  285. {
  286.   register cuc    *t;        /* -> text character being tested */
  287.   register cuc    *p;        /* -> pattern char being tested */
  288.   register cuc    *tx;        /* -> possible start of match */
  289.   register unsigned int    m;      /* length of pattern */
  290.   register cuc    *top;        /* -> high water mark in text */
  291.   unsigned int  shift[UCHAR_MAX + 1];    /* pattern shift table */
  292.  
  293.   if ( s1 == NULL || s2 == NULL )
  294.     return NULL;        /* certainly, no match is found! */
  295.  
  296.   /* Precompute shift intervals based on the pattern;
  297.      the length of the pattern is determined as a side effect: */
  298.  
  299.   bzero(&shift[1], 255);
  300.  
  301.   /* Note: shift[0] is undefined at this point (fixed later). */
  302.  
  303.   for ( m = 1, p = (cuc *)s2; *p != EOS; ++m, ++p )
  304.     shift[(cuc)*p] = m;
  305.  
  306.   {
  307.     register unsigned char c;
  308.  
  309.     c = UCHAR_MAX;
  310.     do
  311.       shift[c] = m - shift[c];
  312.     while ( --c > 0 );
  313.     /* Note: shift[0] is still undefined at this point. */
  314.   }
  315.  
  316.   shift[0] = --m;         /* shift[EOS]; important details! */
  317.  
  318.   /* Try to find the pattern in the text string: */
  319.  
  320.   for ( top = tx = (cuc *)s1; ; tx += shift[*(top = t)] ) {
  321.     for ( t = tx, p = (cuc *)s2; ; ++t, ++p ) {
  322.       if ( *p == EOS )       /* entire pattern matched */
  323.     return (char *)tx;
  324.       if ( *p != *t )
  325.     break;
  326.     }
  327.     if ( t < top ) /* idea due to ado@elsie.nci.nih.gov */
  328.       t = top;       /* already scanned this far for EOS */
  329.     do    {
  330.       if ( *t == EOS )
  331.     return NULL;    /* no match */
  332.     } while ( ++t - tx != m );    /* < */
  333.   }
  334. }
  335.  
  336.